﻿/********************************************************************************************
 * Time de desenvolvedores
 *  Vinícius Batista de Souza       @vbs_br
 *  Fermando Martin Figuera         @fernandomartin
 *  
 *  
 * Jogo desenvolvido para testar algumas features e produzir trilha de treinamento no MSDN.
 * Faça o download do SDK do Windows Phone, faça você também o seu jogo :)
 * 
 * Dúvidas utilizem o Forúm do Windows Phone no MSDN.
********************************************************************************************/



using System;
using System.Windows;
using Microsoft.Phone.Controls;
using System.Windows.Threading;

namespace ComboBUG
{
    public partial class GameOver : PhoneApplicationPage
    {
        private int level;
        private int score;
        Helpers.FacebookShare facebookShare;

        DispatcherTimer time_button_share = new DispatcherTimer();

        #region GameOver
        public GameOver()
        {
            InitializeComponent();
            btshare.Click += new RoutedEventHandler(btshare_Click);

            Loaded += new RoutedEventHandler(GameOver_Loaded);
        }
        #endregion

        #region GameOver_Loaded
        void GameOver_Loaded(object sender, RoutedEventArgs e)
        {

            
            btshare.Content = "Share your score on Facebook";
            btshare.IsEnabled = true;

            if (!string.IsNullOrEmpty(NavigationContext.QueryString["score"]))
                score = int.Parse(NavigationContext.QueryString["score"]);

            if (!string.IsNullOrEmpty(NavigationContext.QueryString["level"]))
                level = int.Parse(NavigationContext.QueryString["level"]);

        }
        #endregion

        #region btshare_Click
        void btshare_Click(object sender, RoutedEventArgs e)
        {
            if (btshare.Content.ToString().ToLower().IndexOf("share") != -1)
            {
                btshare.Content = "Processing...";

                time_button_share.Interval = TimeSpan.FromMilliseconds(250);
                time_button_share.Tick += new EventHandler(time_button_share_Tick);
                time_button_share.Start();

                string shareDescription = string.Format(Helper.shareDescription, score.ToString(), level.ToString(), string.Empty, Helper.urlCentrodeTreinamento);
                facebookShare = new Helpers.FacebookShare(browserAuth, Helper.linkToShare, Helper.shareTitle, shareDescription, Helper.shareImage);

            }
           
        }
        #endregion

        #region time_button_share_Tick
        void time_button_share_Tick(object sender, EventArgs e)
        {
            if (facebookShare.isCompleted)
            {
                btshare.Content = "Completed with success!";
                time_button_share.Stop();
            }

            if (facebookShare.timeout)
            {
                btshare.Content = "Share your score on Facebook";
                time_button_share.Stop();
                MessageBox.Show("Sorry! Facebook's server is not available, check your internet connection and try again!");
            }
        }
        #endregion

        #region OnBackKeyPress
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {

                NavigationService.Navigate(new Uri(string.Format("/MainPage.xaml"), UriKind.Relative));
                e.Cancel = true;
           
        }
        #endregion

        #region btRestart_Click
        private void btRestart_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri(string.Format("/GamePage.xaml"), UriKind.Relative));
        }
        #endregion
    }
}